package com.neusoft.elmboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.elmboot.Dto.FoodDto;
import com.neusoft.elmboot.Vo.FoodVo;
import com.neusoft.elmboot.common.result.Result;
import com.neusoft.elmboot.po.FoodEntity;
import com.neusoft.elmboot.service.FoodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/foods")
@RestController
@Slf4j
@Api(tags = "食品管理")
public class FoodController {

    @Autowired
    private FoodService foodService;

    /**
     * 食品分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation("食品 分页查询")
    @GetMapping("/page")
    public Result<Page> page(Integer page, Integer pageSize,  String foodId,String foodName) {

        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<FoodEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(foodId!=null,FoodEntity::getFoodId,foodId);
        lqw.like( foodName!=null,FoodEntity::getFoodName, foodName);


        foodService.page(pageInfo,lqw);


        return Result.success(pageInfo);

    }

    /**
     * 新增商品
     * @param foodDto
     * @return
     */
    @ApiOperation("新增商品")
    @PostMapping("/create")
    public Result save(@RequestBody  FoodDto foodDto){
        log.info("新增商品,{}",foodDto.toString());


        foodService.add(foodDto);
        return Result.success() ;

    }

    /**
     *
     * @param id
     * @return
     */
    @ApiOperation("回显商品")
    @GetMapping
    public Result<FoodVo> getById(Long id){
        log.info("回显商品,{}",id);
        FoodEntity food = foodService.getById(id);
        FoodVo foodVo = new FoodVo();
        BeanUtils.copyProperties(food,foodVo);


        return Result.success(foodVo);
    }

    /**
     * 编辑员工
     * @param foodDto
     * @return
     */
    @ApiOperation("编辑商品")
    @PutMapping
    public Result update(@RequestBody FoodDto foodDto){
        log.info("编辑商品, {}",foodDto.toString());
        FoodEntity food = new FoodEntity();
        BeanUtils.copyProperties(foodDto, food);
        foodService.updateById(food);
        return Result.success();
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @ApiOperation("删除商品")
    @DeleteMapping
    public Result delete(Long id) {
        log.info("删除商品,{}",id);
        foodService.delete(id);

        return Result.success() ;


    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @ApiOperation("逻辑删除")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable Long id){
        log.info("逻辑删除,{}",id);
        LambdaUpdateWrapper<FoodEntity> luw = new LambdaUpdateWrapper<>();
        luw.eq(FoodEntity::getFoodId, id).set(FoodEntity::getDel, 1);
        foodService.update(luw);





        return Result.success();

    }


    @GetMapping("/listFoodByBusinessId")
    public Result<List<FoodVo>> listFoodByBusinessId(Long businessId){
        LambdaQueryWrapper<FoodEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FoodEntity::getBusinessId, businessId);
        List<FoodEntity> list = foodService.list(lqw);
        List<FoodVo> foodVos = list.stream().map((item) -> {
            FoodVo foodVo = new FoodVo();
            BeanUtils.copyProperties(item, foodVo);
            return foodVo;
        }).collect(Collectors.toList());
        return Result.success(foodVos);



    }








}
